﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Web
{
    public partial class LessonLearning : System.Web.UI.Page
    {
        private B_Dowork doworkService = new B_Dowork();
        B_User userService = new B_User();
        B_Insert_Update_Delete insertUpdate = new B_Insert_Update_Delete();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropdown();
            }
        }

        private void BindDropdown()
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);

            if (currentUser != null)
            {
                var lstLessonLearning = doworkService.GetLessonsLearning(currentUser.UserID);
                ddlLessons.Items.Clear();
                ddlLessons.DataSource = lstLessonLearning;
                ddlLessons.DataValueField = "LessonID";
                ddlLessons.DataTextField = "LessonTitle";
                ddlLessons.DataBind();

                var lstStatistic = doworkService.GetDataStatistic(currentUser.UserID);
                rptStatistic.DataSource = lstStatistic;
                rptStatistic.DataBind();
            }
        }

        protected void BtnSubmitClicked(object sender, EventArgs e)
        {
            string userName = Session["Username"].ToString();
            string userPassword = Session["PassWord"].ToString();
            var currentUser = userService.Selectby_username_password(userName, userPassword);

            if (uploadfile.HasFile)
            {
                if (!string.IsNullOrEmpty(ddlLessons.SelectedItem.Value))
                {

                    // Save file to server
                    uploadfile.SaveAs(Server.MapPath("/FilesUpload/" + uploadfile.FileName));

                    // Save file path to DB
                    var dowork = doworkService.GetById(Convert.ToInt32(btnSubmit.CommandArgument));
                    dowork.LessonId = Convert.ToInt32(ddlLessons.SelectedItem.Value);
                    dowork.UserId = currentUser.UserID;
                    dowork.FileAttach = uploadfile.FileName;

                    insertUpdate.update("Dowork", "DoworkId", dowork, dowork.DoworkId);
                }
                else
                    Response.Write("");
            }

            pnlSubmit.Visible = false;
            pnlStatistic.Visible = true;
            BindDropdown();
        }

        protected void RptStatisticItemCommand(object source, RepeaterCommandEventArgs e)
        {
            pnlSubmit.Visible = true;
            pnlStatistic.Visible = false;

            var dowork = doworkService.GetById(Convert.ToInt32(e.CommandArgument));

            ddlLessons.ClearSelection();
            ddlLessons.Items.FindByValue(dowork.LessonId.ToString()).Selected = true;

            btnSubmit.CommandArgument = e.CommandArgument.ToString();
        }

        protected void RptStatisticItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblFileAttach = (Label)e.Item.FindControl("lblFileAttach");
            var btnSend = (Button)e.Item.FindControl("btnSend");

            if (!string.IsNullOrEmpty(lblFileAttach.Text))
                btnSend.Visible = false;
            else
                btnSend.Visible = true;
        }
    }
}